﻿using Logistics.EntityFrameworkCore;
using Logistics.Job;
using Logistics.Model;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Setup
{
    [ApiExplorerSettings(GroupName = "SD")]
    public class JobAppService : ApplicationService, IJobAppService
    {
        private readonly IRepository<PositionInfo, int> _RoleRepository;
        private readonly LogisticsDbContext _db;

        public JobAppService(IRepository<PositionInfo, int> RoleRepository, LogisticsDbContext db)
        {
            _RoleRepository = RoleRepository;
            _db = db;
        }

        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<PagedResultDto<JobDto>> GetJobAsync(GetJobListDto dto)
        {
            var job = _db.PositionInfo.ToList();
            var bm = _db.Deptinfo.ToList();
            var list = from a in job
                       join b in bm on a.Deptid equals b.Id
                       select new JobDto
                       {
                           Id = a.Id,
                           Name = a.Name,
                           Aliases = a.Aliases,
                           BName = b.Name,
                           CreateTime = a.SetTime
                       };
            if (!string.IsNullOrEmpty(dto.Name))
            {
                list = list.Where(m => m.Name.Contains(dto.Name)).ToList();
            }
            var totalCount = list.Count();
            list = list.Skip(dto.SkipCount).Take(dto.MaxResultCount);
            return new PagedResultDto<JobDto>(totalCount, list.ToList());
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<JobDto> CreateAddAsync(CreateJobDto input)
        {
            var studentDto = new PositionInfo
            {
                Id = 1,
                Name = input.Name,
                Deptid = input.Deptid,
                Aliases = input.Aliases,
                SetTime = DateTime.Now,
            };
            await _RoleRepository.InsertAsync(studentDto);
            return ObjectMapper.Map<PositionInfo, JobDto>(studentDto);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _RoleRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="studentDto"></param>
        /// <returns></returns>
        public async Task<JobDto> GetAsync(int id)
        {
            var student = await _RoleRepository.GetAsync(id);
            return ObjectMapper.Map<PositionInfo, JobDto>(student);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task UpdateAsync(int id, CreateJobDto input)
        {
            var receipts = await _RoleRepository.GetAsync(id);

            receipts.Id = 1;
            receipts.Name = input.Name;
            receipts.Deptid = input.Deptid;
            receipts.Aliases = input.Aliases;
            receipts.SetTime = DateTime.Now;

            await _RoleRepository.UpdateAsync(receipts);

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DeletePdeleteAsync(string ids)
        {
            var a = ids.Split(',');
            foreach (var item in a)
            {
                var list = _db.PositionInfo.Find(Convert.ToInt32(item));
                _db.PositionInfo.RemoveRange(list);
            }

            var i = _db.SaveChanges();
            return i;
        }
    }

}
